Page({

  data: {
    questionnaire: {}
  },

  /**
   * 单选题逻辑实现
   */
  onChangeRadio(event) {
    
    var tmp_questionnaire = this.data.questionnaire;

    for (let key in this.data.questionnaire.questionList[event.target.id].choices) {
      tmp_questionnaire.questionList[event.target.id].choices[key] = 0;
    }
    tmp_questionnaire.questionList[event.target.id].choices[event.detail.value] = 1;

    this.setData({
      questionnaire: tmp_questionnaire
    });
  },

  /**
   * 多选题逻辑实现
   */
  onChangeChecked(event) {
    var tmp_questionnaire = this.data.questionnaire;

    for (let key in this.data.questionnaire.questionList[event.target.id].choices) {
      tmp_questionnaire.questionList[event.target.id].choices[key] = 0;
    }
    for (let value of event.detail.value){
      tmp_questionnaire.questionList[event.target.id].choices[value] = 1;
    }

    this.setData({
      questionnaire: tmp_questionnaire
    });

  },

  //提交调查问卷
  formsubmit: function (e) {
    wx.request({
      url: 'https://dorm.dormassistant.wang:8080/questionnaire/modifyByUserId',
      method: "POST",
      data: {
        userId: this.data.questionnaire.userId,
        questionList: this.data.questionnaire.questionList
      },
      success: function (res) {
        if (res.data.success)
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  url: "./home/home"
                })
              }, 2000)
            }
          });
      },
      fail: function (res) {
        wx.showToast({
          title: "网络故障",
          icon: '',
          duration: 2000
        });
      }
    });
  },

  onLoad: function (options) {
    getQuestionnaire(this);
  },

  onReady: function () {

  }

});


function getQuestionnaire(_this){
  wx.request({
    url: 'https://dorm.dormassistant.wang:8080/questionnaire/findByUserId',
    data: {
      userId: wx.getStorageSync('openid')
    },
    method: "GET",
    success: function (res) {
      console.log(res.data);
      _this.setData({
        questionnaire: res.data.questionnaire
      });
    },
    fail: function (res) {
      wx.showToast({
        title: '网络故障',
        icon: '',
        duration: 2000
      });
    }
  })
}